﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.PageView;

namespace CommunitySystem
{
    public partial class KaoGuanDetailControl : UserControl
    {
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        private ObservableCollection<KaoGuanModel> selectedempmodel =
           new ObservableCollection<KaoGuanModel>();
        private KaoGuanDetailModel model = new KaoGuanDetailModel();
        public ObservableCollection<PublicOfficialInfo> poi = new ObservableCollection<PublicOfficialInfo>();
        public int tiaojian = 0;
        public KaoGuanDetailControl()
        {
            InitializeComponent();
            //for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            //{
            //    ComboBoxItem cbi = new ComboBoxItem();
            //    cbi.Content = i.ToString();
            //    cbi.Tag = i;
            //    nian.Items.Add(cbi);
            //}
            //nian.SelectedIndex = 0;
            
//            Client.GetPublicOfficialInfoCompleted += new EventHandler<GetPublicOfficialInfoCompletedEventArgs>(Client_GetPublicOfficialInfoCompleted);
//            Client.GetPublicOfficialInfoAsync(
//                @"status = 1
//union all 
//select * from PublicOfficialInfo where status = 0 order by inputtime desc");
            Client.GetPublicOfficialInfo1Completed += new EventHandler<GetPublicOfficialInfo1CompletedEventArgs>(Client1_GetPublicOfficialInfo1Completed);
            Client.GetPublicOfficialInfo1Async(1, 0);
            
        }
        void Client1_GetPublicOfficialInfo1Completed(object sender, GetPublicOfficialInfo1CompletedEventArgs e)
        {
            nian.ItemsSource = null;
            poi = e.Result;
            ObservableCollection<PublicOfficialInfo> poi1 = new ObservableCollection<PublicOfficialInfo>();
            //if (poi.Where(p => p.Status != 3).FirstOrDefault() != null) poi1.Add(poi.Where(p => p.Status != 3).FirstOrDefault());
            foreach (PublicOfficialInfo p in poi)
            {
                // if (p.Status == 1 || p.Status == 0) poi1.Add(p);
                if (!p.IsLocked.Value) poi1.Add(p);
            }
            if (poi1.Count > 0 && poi1[0] != null)
            {
                nian.ItemsSource = poi1;
                if (poi1.Count > 0) nian.SelectedIndex = 0;
            }
            Load();
        }
        void Client_GetPublicOfficialInfoCompleted(object sender, GetPublicOfficialInfoCompletedEventArgs e)
        {
            nian.ItemsSource = null;
            poi = e.Result;
            ObservableCollection<PublicOfficialInfo> poi1 = new ObservableCollection<PublicOfficialInfo>();
            //if (poi.Where(p => p.Status != 3).FirstOrDefault() != null) poi1.Add(poi.Where(p => p.Status != 3).FirstOrDefault());
            foreach (PublicOfficialInfo p in poi)
            {
                if (p.Status == 1 || p.Status == 0) poi1.Add(p);
            }
            if (poi1.Count > 0 && poi1[0] != null)
            {
                nian.ItemsSource = poi1;
                if (poi1.Count > 0) nian.SelectedIndex = 0;
            }
            Load();
        }

        private string islock = "0";
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            nian.ItemsSource = null;
            islock = "1";
            ObservableCollection<PublicOfficialInfo> poi1 = new ObservableCollection<PublicOfficialInfo>();
            foreach (PublicOfficialInfo i in poi)
            {
              //  if (i.Status == 3) poi1.Add(i);
                if (i.IsLocked.Value) poi1.Add(i);
            }
            if (poi1.Count > 0 && poi1[0] != null)
            {
                nian.ItemsSource = poi1;
                if (poi1.Count > 0) nian.SelectedIndex = 0;
            }
            Load();
        }

        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            islock = "0";
            ObservableCollection<PublicOfficialInfo> poi1 = new ObservableCollection<PublicOfficialInfo>();
            //if (poi.Where(p => p.Status != 3).FirstOrDefault() != null) poi1.Add(poi.Where(p => p.Status != 3).FirstOrDefault());
            foreach (PublicOfficialInfo p in poi)
            {
               // if (p.Status == 0 || p.Status == 1) poi1.Add(p);
                if (!p.IsLocked.Value) poi1.Add(p);
            }
            if (poi1.Count > 0 && poi1[0] != null)
            {
                nian.ItemsSource = poi1;
                if (poi1.Count > 0) nian.SelectedIndex = 0;
            }
            else
            {
                nian.ItemsSource = null;
                tiaojian = 0;
            }
            Load();
        }

        public void Load()
        {
            Client.GetKaoGuanDetailModelCompleted += new EventHandler<GetKaoGuanDetailModelCompletedEventArgs>(Client_GetKaoGuanDetailModelCompleted);
            Client.GetKaoGuanDetailModelAsync(name.Text, tiaojian, 0, "选调交流");
        }

        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox) sender).SelectedItem != null)
            {
                tiaojian = ((PublicOfficialInfo) (((ComboBox) sender).SelectedItem)).ID;
            }
        }

        void Client_GetKaoGuanDetailModelCompleted(object sender, GetKaoGuanDetailModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
           

            var seljw = this.DGRole.SelectedItem as KaoGuanModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                KaoGuanDetailWindow RMWindow = new KaoGuanDetailWindow(Convert.ToInt32(seljw.ID));
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的考官信息！"); 
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as KaoGuanModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Client.DeteleKaoGuanDetailModelCompleted -= new EventHandler<DeteleKaoGuanDetailModelCompletedEventArgs>(Client_DeteleKaoGuanDetailModelCompleted);
                Client.DeteleKaoGuanDetailModelCompleted += new EventHandler<DeteleKaoGuanDetailModelCompletedEventArgs>(Client_DeteleKaoGuanDetailModelCompleted);
                Client.DeteleKaoGuanDetailModelAsync(Convert.ToInt32(seljw.ID));
            }
        }

        void Client_DeteleKaoGuanDetailModelCompleted(object sender, DeteleKaoGuanDetailModelCompletedEventArgs e)
        {
            Load();
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        /// <summary>
        /// 考官通知导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private WordActionService.WordActionClient _client = new WordActionService.WordActionClient();
        private void btnKGTZ_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.SelectedItem != null)
            {
                if (_client != null)
                {

                    _client.EmilyExportInfoCompleted += (sender1, e1) =>
                    {
                        //保存结果
                        if (e1.Error == null)
                        {
                            try
                            {
                                string serverpath = _client.InnerChannel.Via.AbsoluteUri.Replace(_client.InnerChannel.Via.AbsolutePath, "");
                                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(serverpath + e1.Result, UriKind.RelativeOrAbsolute));
                            }
                            catch (Exception ex)
                            {
                                SilverlightMessageBoxLibrary.Message.ErrorMessage("导出失败,可能原因:\n" + ex.Message);
                            }
                        }
                        else
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage("导出失败,可能原因:\n" + e1.Error);
                        }
                    };
                }
                foreach (var item in DGRole.SelectedItems)
                {
                    var entity = item as KaoGuanModel;
                    DateTime dt = (DateTime)entity.RiQi;
                    string dayOfWeek = null;
                    switch (dt.DayOfWeek)
                    {
                        case DayOfWeek.Monday:
                            dayOfWeek = "周一";
                            break;
                        case DayOfWeek.Tuesday:
                            dayOfWeek = "周二";
                            break;
                        case DayOfWeek.Wednesday:
                            dayOfWeek = "周三";
                            break;
                        case DayOfWeek.Thursday:
                            dayOfWeek = "周四";
                            break;
                        case DayOfWeek.Friday:
                            dayOfWeek = "周五";
                            break;
                        case DayOfWeek.Saturday:
                            dayOfWeek = "周六";
                            break;
                        case DayOfWeek.Sunday:
                            dayOfWeek = "周七";
                            break;

                    }
                    //  int hour = int.Parse(entity.Shi.Split(':')[0]);
                    _client.EmilyExportInfoAsync(new ObservableCollection<string>(){
                        entity.Name,entity.KGType
                    }, 0);
                    System.Threading.Thread.Sleep(new TimeSpan(0, 0, 0, 1, 0));
                }
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请至少选中一条记录");
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (KaoGuanModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }

        private void btnlbsz_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (KaoGuanModel i in (ObservableCollection<KaoGuanModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if(selectedempmodel.Count>0)
            {
                string kgdids = "";
                foreach (var item in selectedempmodel)
                {
                    kgdids += item.ID.ToString() + ",";
                }
                KaoGuanDetailWindows RMWindow = new KaoGuanDetailWindows(kgdids.TrimEnd(','));
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();
            }
        }
    }
}
